//
//  AFNTool.m
//  Sneaker
//
//  Created by YouLu on 15/6/29.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "AFNTool.h"

@implementation AFNTool
+(void)get:(NSString *)url Body:(id)body HttpResult:(HTTPStyle)style success:(void(^)(id result))success failure:(void(^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    switch (style) {
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            break;
        case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    [manager.requestSerializer setValue:@"Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12A405 lolapp/3.4.1.550 Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];

    
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
        
    }];
}


+(void)post:(NSString *)url Body:(id)body HttpResult:(HTTPStyle)style RequsetBodyStyle:(RequsetBodyStyle)bodyStyle
    httpHead:(NSDictionary *)dic
    success:(void(^)(id result))success failure:(void(^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 判断网络请求头的字典是否为空
    if (dic) {
        // 不为空遍历字典
        for (NSString *key in dic.allKeys) {
            // 设置为HTTP请求头
            [manager.requestSerializer setValue:dic[key] forHTTPHeaderField:key];
        }
    }

    [manager.requestSerializer setValue:@"3" forHTTPHeaderField:@"client"];
    [manager.requestSerializer setValue:@"562" forHTTPHeaderField:@"version"];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    
    switch (style) {
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            break;
        case XML    :
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    switch (bodyStyle) {
        case StringBody:
            // 请求参数原样传给服务器
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError **error) {
                return parameters;
            }];
            break;
        case JSONBody:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        default:
            break;
    }
    
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
        
    }];
}
@end
